
from django.shortcuts import render, HttpResponse, redirect, reverse
from scripts.init_price_police import S28
from scripts.cos_upload_demo import delete_bucket

def setting(request,project_id):
    return render(request, 'setting.html')


def delete_setting(request, project_id):
    print('-------------------------------------------处理删除项目---------------------------------------------')
    if request.method == 'GET':
        return render(request, 'setting_delete.html')
    project_name = request.POST.get('project_name')
    creater_pwd = request.POST.get('creater_pwd')
    project = S28().search('project', {'project_name': str(project_name)})
    if len(project) == 0:
        return render(request, 'setting_delete.html', {'project_name_error': '项目的名字不正确'})
    create_id = project[0][6]
    pwd = S28().search('user_information', {'id': str(create_id)})
    if pwd[0][2] == creater_pwd:
        print('删除项目和文件以及文档 和相关的cos桶里的文件 最后再删除桶')
        # f返回一个重定向 project_list
        delete_bucket('{}-1302094470'.format(project_id), 'ap-beijing')
        S28().delete_use('project', {'id': '{}'.format(str(project_id))})
        S28().delete_use('wiki', {'project_id': '{}'.format(project_id)})
        S28().delete_use('file_repository', {'project_id': '{}'.format(project_id)})
        return redirect(reverse('project_list'))
    else:
        return render(request, 'setting_delete.html', {'project_name_error': '', 'pwd': '密码不正确'})